//
//  BRAPPViewModel.m
//  template
//
//  Created by SoXun on 2018/7/12.
//  Copyright © 2018年 soxun. All rights reserved.
//

#import "BRAPPViewModel.h"
#import "UserModel.h"
//#import <TencentOpenAPI/TencentOAuth.h>
//#import "WXApi.h"
//#import "WeiboSDK.h"
//#import "SharedView.h"
//#import <UMCommon/UMCommon.h>
#import <UserNotifications/UserNotifications.h>
//#import <UMPush/UMessage.h>
//#import "EnterpriseServeVC.h"
//#import "CompanyDetailsVC.h"
#import "AppDelegate.h"
//#import "ParkLiveFacilitiesVC.h"
//#import "ParkIntroduceVC.h"
//#import <Bugly/Bugly.h>

@implementation BRAPPViewModel
static BRAPPViewModel *appView = nil;
+(BRAPPViewModel *)shareApp{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        appView = [[BRAPPViewModel alloc] init];
    });
    return appView;
}
- (void)finishLaunchOption:(NSDictionary *)option{
//    [WXApi registerApp:WeChatAppID];
//    [WeiboSDK registerApp:WeiBoApp_key];
//    [WeiboSDK enableDebugMode:YES];
//    [Bugly startWithAppId:@"e2804eade5"];
//    if ([UserModel getUserModel]) {
//        [Bugly setUserValue:[UserModel getUserModel].username forKey:@"phone"];
//    }
// TencentOAuth * hh = [[TencentOAuth alloc] initWithAppId:QQAppId andDelegate:[SharedView SharedView]];
}

+ (UserModel *)getUserModel{
     NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
 NSData * data = [user dataForKey:@"loginUser"];
    if (data == nil) {
        return nil;
    }
  UserModel * model =   [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return model;
}

/**
 @param request 判断APP里面有没有功能模块,并且挑战到对应的页面
 */
+ (BOOL)HTMLToViewController:(NSURLRequest *)request{
    NSString * url = [NSString stringWithString:[[request.URL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    if ([url containsString:@"wanyang="]) {
         NSRange range;
        range = [url rangeOfString:@"wanyang="];
        NSString * vcNameCOM = [url substringFromIndex:range.location + range.length];
       NSString * vcName = [vcNameCOM stringByReplacingOccurrencesOfString:@".com" withString:@""];
//        NSLog(@"%@",vcName);
        UIViewController * SeleVc = [self topViewController];
        
//        园区手册、园区企业、园区生活、园区招商、求职招聘、政策法规、金融/制造、企服中心、物业服务、企业服务、园区通知、访客登记、我的订单、我的收藏、我的论坛、我的地址、商家入驻、绑定园区账号、设置
        UIViewController * tageVc;
        if ([vcName isEqualToString:@"园区手册"]) {
            tageVc = [self ClassFormString:@"GardenManualVC"];
        }else if ([vcName isEqualToString:@"园区企业"]){
            tageVc = [self ClassFormString:@"ParkCompanyListVC"];
        }else if ([vcName isEqualToString:@"园区生活"]){
            tageVc = [self ClassFormString:@"ParkLiveVC"];
        }else if ([vcName isEqualToString:@"园区招商"]){
            tageVc = [self ClassFormString:@"GardenAttracNnvestmentVC"];
        }else if ([vcName isEqualToString:@"求职招聘"]){
            tageVc = [self ClassFormString:@"JobVC"];
        }else if ([vcName isEqualToString:@"政策法规"]){
            tageVc = [self ClassFormString:@"PolicyRegulationVC"];
        }else if ([vcName isEqualToString:@"金融/制造"]){
            tageVc = [self ClassFormString:@"FinancialVC"];
        }else if ([vcName isEqualToString:@"企服中心"]){
            tageVc = [self ClassFormString:@"BRCompanyServeVC"];
        }else if ([vcName isEqualToString:@"物业服务"]){
//            tageVc = BRLoadStoryboardVC(@"Serve", @"EnterpriseServeVC")
//            [(EnterpriseServeVC *)tageVc setTypeId:@"1"];
        }else if ([vcName isEqualToString:@"企业服务"]){
//            tageVc = BRLoadStoryboardVC(@"Serve", @"EnterpriseServeVC")
//             [(EnterpriseServeVC *)tageVc setTypeId:@"2"];
        }else if ([vcName isEqualToString:@"园区通知"]){
//            tageVc = BRLoadStoryboardVC(@"Serve", @"GardenInformVC")
        }else if ([vcName isEqualToString:@"访客登记"]){
//            tageVc = BRLoadStoryboardVC(@"Serve", @"VisitorRegisterVC")
        }
        else if ([vcName isEqualToString:@"我的订单"]){
            tageVc =[self ClassFormString:@"MyOrderListVC"];
        }else if ([vcName isEqualToString:@"我的收藏"]){
            tageVc =[self ClassFormString:@"MyCollectVC"];
        }else if ([vcName isEqualToString:@"我的论坛"]){
            tageVc =[self ClassFormString:@"MyForumListVC"];
        }else if ([vcName isEqualToString:@"我的地址"]){
            tageVc =[self ClassFormString:@"MyAddressVC"];
        }else if ([vcName isEqualToString:@"商家入驻"]){
            tageVc =[self ClassFormString:@"BRShopApplyForVC"];
        }else if ([vcName isEqualToString:@"绑定园区账号"]){
           
         
        }else if ([vcName isEqualToString:@"设置"]){
            tageVc = [self ClassFormString:@"BRSettingVC"];
        }
        if (SeleVc && tageVc) {
            [SeleVc.navigationController pushViewController:tageVc animated:YES];
        }
        return NO;
    }
  
    return YES;
}
+ (UIViewController *)ClassFormString:(NSString *)string{
    return [[NSClassFromString(string) alloc] init];
}
+ (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

+ (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

+ (UIViewController *)BannerToViewController:(NSDictionary *)dict{
//    if (kDictIsEmpty(dict)) {
//        return nil;
//    }
    NSString * hrefType = dict[@"hrefType"];
    if (!hrefType) {
        return nil;
    }
    int href = [hrefType intValue];
    UIViewController * vc;
    UITabBarController * tab;
    switch (href) {
        case 0:
            //最新资讯
            vc = [[NSClassFromString(@"GardenInformVC") alloc] init];
            [vc setValue:@"XXX" forKey:@"tempString"];
            break;
        case 1:
            //园区手册
            vc =  [[NSClassFromString(@"GardenManualVC") alloc] init];
            break;
            
        case 2:
            //企业列表
            vc = [[NSClassFromString(@"ParkCompanyListVC") alloc] init];

            break;
        case 3:
//            政策法规
              vc =  [[NSClassFromString(@"PolicyRegulationVC") alloc] init];
            break;
            
        case 4:
            // 企服中心
             vc =  [[NSClassFromString(@"BRCompanyServeVC") alloc] init];
            break;
            
        case 5:
               // 园区招商
               vc =  [[NSClassFromString(@"GardenAttracNnvestmentVC") alloc] init];
            break;
            
        case 6:
          //论坛
            tab = (UITabBarController *)(AppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController;
            tab.selectedIndex = 2;
            break;
            
        case 7:
            tab = (UITabBarController *)(AppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController;
            tab.selectedIndex = 1;
            break;
            
       
        case 8:
        tab = (UITabBarController *)(AppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController;
            if (tab.selectedIndex != 0) {
                tab.selectedIndex = 0;
            }else{
                UIViewController * vc = [self topViewController];
                [vc.navigationController popToRootViewControllerAnimated:YES];
                 tab.selectedIndex = 0;
            }
            
            break;
            
        case 9:
            // 文章详情
            vc = [self ClassFormString:@"ParkLiveFacilitiesVC"];
//            [(ParkLiveFacilitiesVC *)vc setContent:dict[@"content"]];
            break;
        case 10:
            // 园区生活
            vc = [self ClassFormString:@"ParkLiveVC"];
            break;
            case 11:
            //不跳转
            break;
         
        
            case 12:
            // 设置
            vc = [self ClassFormString:@"BRSettingVC"];
            break;
            
        case 13:
            // 求职招聘
            vc = [self ClassFormString:@"JobVC"];
            break;
            
            case 14:
            // 金融制造
            vc = [self ClassFormString:@"FinancialVC"];
            break;
            
            
            case 15:
            // 园区通知
            vc = [self ClassFormString:@"GardenInformVC"];
            break;
            case 16:
            // 访客登记
//            vc = BRLoadStoryboardVC(@"Serve", @"VisitorRegisterVC");
            break;
            
            case 17:
            //我的订单
            vc = [self ClassFormString:@"MyOrderListVC"];
            
            break;
            
            case 18:
            // 我的收藏
            vc = [self ClassFormString:@"MyCollectVC"];
            break;
            
            case 19:
            //我的地址
            vc = [self ClassFormString:@"MyAddressVC"];
            break;
            
            case 20:
            // 商家入驻
            vc = [self ClassFormString:@"MyShopAddVC"];
            break;
            
            case 21:
            //绑定园区账号
            vc = [self ClassFormString:@"alreadyGardenVC"];
            break;
         
    }
    return vc;
}

+(void)TitleRecommendDidSelectWithDict:(NSDictionary *)dict{
    UIViewController * SelfVC =  [self topViewController];
//    ParkIntroduceVC * vc = [[ParkIntroduceVC alloc] init];
//    vc.requsrtUrl = @"news/getCateDetail";
//    vc.RequestDict = @{@"id":dict[@"id"]};
//    vc.type = 5;
//    vc.IDString = dict[@"id"];
//    [SelfVC.navigationController pushViewController:vc animated:YES];
}
@end
